package com.example.administrator.base.activity;


import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bigkoo.pickerview.OptionsPickerView;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.AreaBean;
import com.example.administrator.base.bean.ModelAreaBean;
import com.example.administrator.base.bean.ReLogin;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GetJsonDataUtil;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SecretUtils;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegiestTwo extends BaseActivity {
    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.tv_city)
    TextView tvCity;
    @InjectView(R.id.rl_one)
    RelativeLayout rlOne;
    @InjectView(R.id.et_area)
    EditText etArea;
    private ArrayList<ModelAreaBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
    private boolean isLoaded = false;
    ArrayList<ModelAreaBean> pro =new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regiest_two);
        ButterKnife.inject(this);
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("注册");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {

    }


    private void ShowPickerView() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, android.view.View v) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1).getPickerViewText() +
                        options2Items.get(options1).get(options2) +
                        options3Items.get(options1).get(options2).get(options3);
                tvCity.setText(tx);
            }
        })

                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();

        pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvOptions.show();
    }


    private void initJsonData() {//解析数据
        String JsonData = new GetJsonDataUtil().getJson(this, "province.json");//获取assets目录下的json文件数据
        AreaBean areaBean=GsonTools.changeGsonToBean(JsonData,AreaBean.class);
       int x= areaBean.getDistrict().size();
        for (int i=0;i<x;i++){

            if (areaBean.getDistrict().get(i).getSysAreaParentId()==1){
                int sysAreaId = areaBean.getDistrict().get(i).getSysAreaId();
                String areapro=areaBean.getDistrict().get(i).getSysAreaName();
                ArrayList<ModelAreaBean.CityBean> city=new ArrayList<>();
                for (int y=0;y<x;y++ ){

                    if (areaBean.getDistrict().get(y).getSysAreaParentId()==sysAreaId){
                        int areaId = areaBean.getDistrict().get(y).getSysAreaId();
                        String areacity=areaBean.getDistrict().get(y).getSysAreaName();

                        ArrayList<String> area=new ArrayList<>();
                         for (int z=0;z<x; z++){
                             if (areaBean.getDistrict().get(z).getSysAreaParentId()==areaId){
                                 String name = areaBean.getDistrict().get(z).getSysAreaName();
                                 area.add(name);
                             }
                         }
                         city.add(new ModelAreaBean.CityBean(areacity,area));
                    }
                }

                pro.add(new ModelAreaBean(areapro,city));
            }

        }
        Gson gson=new Gson();
        String s = gson.toJson(pro);
        Log.i("1415451012",""+s);
        options1Items=pro;
        for (int i=0;i<pro.size();i++){//遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）

            for (int c=0; c<pro.get(i).getCity().size(); c++){//遍历该省份的所有城市
                String CityName = pro.get(i).getCity().get(c).getName();
                CityList.add(CityName);//添加城市

                ArrayList<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (pro.get(i).getCity().get(c).getArea() == null
                        ||pro.get(i).getCity().get(c).getArea().size()==0) {
                    City_AreaList.add("");
                }else {

                    for (int d=0; d < pro.get(i).getCity().get(c).getArea().size(); d++) {//该城市对应地区所有数据
                        String AreaName = pro.get(i).getCity().get(c).getArea().get(d);

                        City_AreaList.add(AreaName);//添加该城市所有地区数据
                    }
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }
            options2Items.add(CityList);
            options3Items.add(Province_AreaList);
        }
        showToast("数据解析完成");
        isLoaded=true;
    }


    @OnClick({R.id.rl_one, R.id.btn_next2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_one:
                if (isLoaded){
                    ShowPickerView();
                }else {
                    showToast("数据加载中，请稍等");
                }
                break;
            case R.id.btn_next2:
              GetArea();
                break;
        }
    }
    /*获取区域列表*/
    public void GetArea() {
        String id ="99bicycle1314";
        String s;
        try {
            s = SecretUtils.Encrypt3DES(id);
            String s1 = SecretUtils.Decrypt3DES(s);
            Log.i("2122154",""+s1+"密码："+s);
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("action", "GETALLDISTRICT")
                .add("secMsg", s)
                .build();
        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/operationpersonnew?")
                .post(formBody)
                .build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String string = response.body().string();
                        final ReLogin mes = GsonTools.changeGsonToBean(string, ReLogin.class);
                        Log.i("21584212", "1" + string);
                        if (mes != null) {
                            if (mes.getCode() == 1) {


                            } else {

                            }
                        } else {

                        }
                    } else {
                        throw new IOException("Unexpected code " + response);

                    }
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        }).start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
